package com.swanclouds.redfish.systems;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.swanclouds.api.Apis;
import com.swanclouds.api.systems.EthernetInterfaceService;
import com.swanclouds.api.systems.VLANService;
import com.swanclouds.core.transport.ClientConstants;
import com.swanclouds.model.common.redfish.CollectionResource;
import com.swanclouds.model.systems.EthernetInterfaceConfigure;
import com.swanclouds.model.task.Task;
import com.swanclouds.redfish.common.redfish.CollectionResourceModel;
import com.swanclouds.redfish.internal.BaseSystemService;
import com.swanclouds.redfish.systems.domain.EthernetInterface;
import com.swanclouds.redfish.task.domain.TaskModel;

import java.util.List;

/**
 * @author 张立强
 * @version jdk1.8.0
 */
public class EthernetInterfaceServiceImpl extends BaseSystemService implements EthernetInterfaceService {


    /**
     * 查询服务器主机以太网集合资源的信息
     * @return the EthernetInterface resource info
     */
    @Override
    public CollectionResource get() {
        return get(CollectionResourceModel.class, uri(ClientConstants.SYSTEMS_ETHERNET_INTERFACES, getSystemId())).execute();
    }

    /**
     * 查询以太网资源信息
     * @param ethernetInterfaceId 以太网ID
     * @return EthernetInterface
     */
    @Override
    public EthernetInterface getEthernetInterface(String ethernetInterfaceId) {
        return get(EthernetInterface.class, uri(ClientConstants.SYSTEMS_ETHERNET_INTERFACES_ID, getSystemId(), ethernetInterfaceId)).execute();
    }

    /**
     * 仅支持Atlas系列产品，且需配合iBMA使用，iBMA版本号需大于或等于1.50.831。
     * X86系统是EulerOS且内核版本是3.10.0-327或3.10.0-514
     *
     * @param configure 配置信息
     * @param ethernetInterfaceId 以太网ID
     * @return Task
     */
    @Override
    public Task configure(EthernetInterfaceConfigure configure, String ethernetInterfaceId) {
        Preconditions.checkNotNull(ethernetInterfaceId, "EthernetInterface Id is null");
        return post(TaskModel.class, uri(ClientConstants.SYSTEMS_ETHERNET_INTERFACES_ID_CONFIGURE, getSystemId(), ethernetInterfaceId)).execute();
    }

    /**
     * 查询以太网资源信息列表
     *
     * @return List&lt;EthernetInterface&gt;
     */
    @Override
    public List<EthernetInterface> list() {
        List<EthernetInterface> ethernetInterfaceList = Lists.newArrayList();
        CollectionResource resource = get();
        resource.getCollectionResourceList().stream().forEach(ref -> {
            ethernetInterfaceList.add(get(EthernetInterface.class, uri(ref)).execute());
        });
        return ethernetInterfaceList;
    }

    /**
     * VLAN 集合资源服务
     *
     * @return VLANService
     */
    @Override
    public VLANService VLAN() {
        return Apis.get(VLANService.class);
    }

}
